package com.lz.football.module.ladderMatch.exchange
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.TipsConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.LadderEvent;
	import com.liangzhong.events.StadiumEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.managers.TipsManager;

	/**
	 *荣誉兑换面板 
	 * @author Administrator
	 * 
	 */	
	public class ExchangePanel extends ExchangeBasePanel//TitleWindow implements IComponent
	{
//		/**
//		 *显示长度 
//		 */
//		private const ITEMLENGTH:int = 9;
//		private var symbolSkin		:MovieClip;//皮肤
//		/**
//		 *关闭按钮  
//		 */
//		private var closeBtn:DisplayObject;
//		/**
//		 *item容器 
//		 */
//		private var _contain:MovieClip;
//		/**
//		 *换页  
//		 */
//		private var _page:PagePanel;
//		/**
//		 *兑换按钮 
//		 */
//		private var _exchangeBtn:DisplayObject;
		/**
		 *天梯积分 
		 */
		private var _ladderIntegral:TextField;
		/**
		 *荣誉积分 
		 */
		private var _honourValue:TextField;
//		/**
//		 * 
//		 */
//	
//		private var _goodVec:Array;
//		/**
//		 *选中的item 
//		 */
//		private var _selectItem:ExchangeItem;
//		/**
//		 *页容器 
//		 */
//		private var _pageContain:MovieClip;
		/**
		 *荣誉图标  
		 */
		private var _honourIcon:MovieClip;
		public function ExchangePanel()
		{
			super();
//			initUI();
		}
		
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.football.stadium.exchange");
			this.addChild(symbolSkin);
			super.initUI();
			_windowBg = symbolSkin.echangeBg;
			isDrag = true;
//			setCloseBtn();
//			closeBtn = symbolSkin.closeBtn;
//			_contain = symbolSkin._contain;
//			_pageContain = symbolSkin._pageContain;
//			_page = new PagePanel();
//			_page.callBack = changePage;
//			_pageContain.addChild(_page);
//			_exchangeBtn = symbolSkin._exchangeBtn;
			_ladderIntegral = symbolSkin._ladderIntegral;
			_honourValue = symbolSkin._honourValue;
			_honourIcon = symbolSkin._honourIcon;
			_viewHeight = symbolSkin.height;
			_viewWidth = symbolSkin.width;
			moveClose(510,5);
			addEvtListener();
			onResizeHandler();
		}
		/**
		 *换页 
		 * @param $str
		 * 
		 */
		override public function changePage($str:String):void
		{
			showItem();
		}
//		
		override public function addEvtListener():void
		{
//			closeBtn.addEventListener(MouseEvent.CLICK,oncloseHandler);
			super.addEvtListener();
			_honourIcon.addEventListener(MouseEvent.ROLL_OUT,onIconOutHandler);
			_honourIcon.addEventListener(MouseEvent.ROLL_OVER,onIconOverHandler);
		}
		protected function onIconOverHandler(event:MouseEvent):void
		{
			TipsManager.show(TipsConst.PLAYERPROPERTYTIPS,tipString());
		}
		private function tipString():Object
		{
			return null//{base:"荣誉："+AppData.getInstance().clubDatamanager.clubInfoVo.tiantipoint};
		}
		protected function onIconOutHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			TipsManager.hide();
		}
		/**
		 *兑换 
		 * @param event
		 * 
		 */
		override public function onExchangeHandler(event:MouseEvent):void
		{
			if(_selectItem == null)
			{
				Alert.showErrorTip("请选择要兑换的物品");
				return;
			}
			if(_selectItem.goodVo.honour > int(_honourValue.text))
			{
				Alert.showErrorTip("荣誉不足，不可以购买此物品");
				return;
			}
			Alert.show("是否花"+_selectItem.goodVo.honour+ "荣誉兑换" + _selectItem.goodVo.name,"",Alert.NO,GlobalDef.AlertContainer,exchageAlert);
			
		}
		private function exchageAlert($type:int):void
		{
			if($type == Alert.YES)
				AppEventDispatcher.getInstance().dispatchEvent(new LadderEvent(LadderEvent.EXCHANGE,{pid:_selectItem.goodVo.pid,type:_selectItem.goodVo.type,num:1}));
		}
//		override protected function onCloseHandler(event:MouseEvent):void
//		{
//			
//			close();
//		}
		/**
		 *移除 
		 * 
		 */
//		public function close():void
//		{
//			if(this.parent)this.parent.removeChild(this);
//		}
		/**
		 *显示 
		 * 
		 */
		public function show($integral:int,$tiantipoint:int):void
		{
			_ladderIntegral.text = $integral.toString();
			_honourValue.text = $tiantipoint.toString();
//			GlobalDef.GameContainer.addChild(this);
//			this.x = (GlobalDef.GameWidth - this.width)>>1;
//			this.y = (GlobalDef.GameHeight - this.height)>>1;
			setItem();
			var arr:Array = AppData.getInstance().goodsManager.materials;
			if(_goodVec == null)_goodVec = arr.filter(filterGood);
			
			
			_page.max = Math.ceil(_goodVec.length / ITEMLENGTH);
			_page.min = 1;
			showItem();
			
		}
		private function filterGood(item:GoodsVo, index:int, array:Array):Boolean
		{
			if(item.honour > 0)return true;
			
			return false;
		}
		/**
		 *返回荣誉兑换 
		 * @param $integral
		 * @param $tiantipoint
		 * 
		 */
		public function exchange($integral:int,$tiantipoint:int):void
		{
			_ladderIntegral.text = $integral.toString();
			_honourValue.text = $tiantipoint.toString();
		}
		
		
		/**
		 * 重置 
		 * 
		 */
		override public function reset():void
		{
			for(var i:int = 0;i < ITEMLENGTH;i++)
			{
				_itemVec[i].reSet();
			}
			_selectItem = null;
		}
		override public function removeEvtListener():void
		{
//			closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			_exchangeBtn.removeEventListener(MouseEvent.CLICK,onExchangeHandler);
			_honourIcon.removeEventListener(MouseEvent.ROLL_OUT,onIconOutHandler);
			_honourIcon.removeEventListener(MouseEvent.ROLL_OVER,onIconOverHandler);
		}
		
//		override public function destroyView():void
//		{
//			removeEvtListener();
//			if(this.parent)
//				this.parent.removeChild(this);
//			while(_itemVec.length)
//			{
//				_itemVec.pop().destroyView();
//			}
//			_goodVec.splice(0,_goodVec.length);
//			_goodVec = null;
//			_page.destroyView();
//			closeBtn = null;
//			symbolSkin = null;
//		}
	}
}